package fileSystem;
import java.io.*;
import java.util.Scanner;
public class FileSystem{
	static File file1;
	static byte[] b;
    public void showDir(String path){
		if(path.length()==2&&path.endsWith(":")){
			path=path+"\\";
		}
		File dir=new File(path); 
        File fileName[]=dir.listFiles();
        for(int i=0;i<fileName.length;i++){
			System.out.printf("\n文件名称:%s,长度:%d",
					fileName[i].getName(),fileName[i].length());
        }
	}
	public void fileDelete(String s,String path){
		s=path+"\\"+s;
		File file=new File(s);
		file.delete();
		System.out.println("文件已删除成功！");
	}
	public void fileLook(String s,String path){
		s=path+"\\"+s;
		File readFile=new File(s);
       
        try{ FileReader inOne=new FileReader(readFile);
             BufferedReader inTwo= new BufferedReader(inOne);
             
             String s3=null;
             
            
             while((s3=inTwo.readLine())!=null){
                System.out.println(s3);
             } 
             inOne.close();
             inTwo.close();
        }
        catch(IOException e){
             System.out.println(e);
        }  
	}
	public void runFile(String s,String path){
		s=path+"\\"+s;
		try{  
			Runtime ce=Runtime.getRuntime();
            File file=new File(s);
            ce.exec(file.getAbsolutePath());
        }
        catch(Exception e){} 
    }


	public void fileCopy(String s,String path){
		
		s=path+"\\"+s;
		file1=new File(s);
		b=new byte[(int)file1.length()];
		try{
			FileInputStream in=new FileInputStream(file1);
			
			while((in.read(b))!=-1){
			}
			in.close();
		}
		catch(IOException e){}

		System.out.println("文件已复制到剪贴板！");
	}


	public void filePaste(String path){
		
		
		File file=new File(path,file1.getName());
		try{
			FileOutputStream out=new FileOutputStream(file);
			out.write(b);
			out.close();
		}
		catch(IOException e){}
		System.out.println("文件已粘贴到当前文件夹！");
	}


	public void createDir(String s,String path){
		s=path+"\\"+s;
		File file=new File(s);
		file.mkdir();
		System.out.println("目录已创建成功！");
		
	}

	public void createFile(String s,String name){
		
		File file=new File(s,name);
		try{
			if(file.createNewFile()){
				System.out.println("文件已创建成功！");
			}
			else{
				System.out.println("文件创建失败！");
			}
		}
		catch(IOException e){}
	}
	public String changePath(String s2,String s3){
		if(s3.length()==2&&s3.endsWith(":")){
			s3=s3+"\\";
		}
		
		Scanner scanner=new Scanner(s2);
		String s1=scanner.next();
		String s=scanner.next();
		
		File dir=new File(s3); 
        File fileName[]=dir.listFiles();
		int i;
        for(i=0;i<(int)fileName.length;i++){
			
            if(s.equals(fileName[i].getName())){
				s3=s3+"\\"+s;
				break;
			}	
		}
		if(i==(int)fileName.length){
			System.out.println("系统找不到指定的路径！");
		}
		return s3;
	}
	public String f(String s2){
		Scanner scanner=new Scanner(s2);
		String s1=scanner.next();
		String s=scanner.next();
		return s;
	}
	public String returnParent(String s){
		int m=s.lastIndexOf("\\");
		s=s.substring(0,m);
		return s;
	}
}
